package com.uml.umlwork.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uml.umlwork.dto.course.select.InputScoreDTO;
import com.uml.umlwork.entity.Course;
import com.uml.umlwork.entity.CourseSelect;
import com.uml.umlwork.mapper.CourseSelectMapper;
import com.uml.umlwork.service.CourseSelectService;
import com.uml.umlwork.utils.DataUtil;
import com.uml.umlwork.utils.PageResult;
import com.uml.umlwork.vo.course.select.CourseSelectVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 少枫
 * @date 2021/1/5
 */
@Service
public class CourseSelectServiceImpl implements CourseSelectService {

    private final CourseSelectMapper courseSelectMapper;

    public CourseSelectServiceImpl(CourseSelectMapper courseSelectMapper) {
        this.courseSelectMapper = courseSelectMapper;
    }

    @Override
    public PageResult<CourseSelectVO> getAllByClassId(Long current, Long size, Integer classId) {
        //根据classId查询选课信息及学生信息
        Page<CourseSelectVO> page = courseSelectMapper.getAllByCondition(new Page<>(current, size),
                new QueryWrapper<CourseSelect>().lambda().eq(CourseSelect::getClassId, classId));
        PageResult<CourseSelectVO> result = new PageResult<>();
        DataUtil.pageFormat(page, result);
        return result;
    }

    @Override
    public void inputScore(InputScoreDTO dto) {
        CourseSelect cs = courseSelectMapper.selectById(dto.getId());
        cs.setScoreLab(dto.getScoreLab())
                .setScoreTheory(dto.getScoreTheory());
        courseSelectMapper.updateById(cs);

    }

    @Override
    public PageResult<CourseSelectVO> getAllCourseSelects(Long current, Long size, Integer id) {

        Page<CourseSelectVO> page = courseSelectMapper.getAllByCondition(new Page<>(current, size),
                new QueryWrapper<CourseSelect>().lambda().eq(CourseSelect::getStudentId, id));
        PageResult<CourseSelectVO> result = new PageResult<>();
        DataUtil.pageFormat(page, result);
        return result;
    }

    @Override
    public void saveStatus(Integer id, Integer confirm, Integer open) {
        CourseSelect cs = courseSelectMapper.selectById(id);
        cs.setHasConfirm(confirm.equals(1)).setHasLosed(open.equals(1));
        courseSelectMapper.updateById(cs);
    }
}
